import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import com.acme.bankapp.domain.Bank;
import com.acme.bankapp.service.BankService;
import com.acme.bankapp.domain.Gender;

public class SystemTest {
	static Bank bank = new Bank();
	public static void main(String[] args) throws IOException {
		BankService service = new BankService();
		try {
			String s1 = "accounttype=s;balance=10;overdraft=0;firstname=A;lastname=A1;gender=m;";
			String s2 = "accounttype=s;balance=11;overdraft=-70;firstname=B;lastname=B1;gender=f;";
			String s3 = "accounttype=s;balance=10;overdraft=12;firstname=C;lastname=C1;gender=m;";
			String s4 = "accounttype=s;balance=10;overdraft=0;firstname=D;lastname=D1;gender=m;";
			PrintWriter out = new PrintWriter(new BufferedWriter (new FileWriter("Bank.txt")));
			out.println(s1);
			out.println(s2);
			out.println(s3);
			out.println(s4);
			out.close();
		}
		catch (Exception e) {
			System.out.println("Error");
			return;
		}
		try {
			BufferedReader in = new BufferedReader(new FileReader("Bank.txt"));
			String data = new String();
			do {
				data = in.readLine();
				if (data != null) {
					service.addClient(bank, data);
				}
			} while (data != null);
			in.close();
		}
		catch (IOException e) {
			System.out.println("Error in reading the file");
			return;
		}
		System.out.format("\n");
		System.out.println("Before deserialization: ");
		service.printAllClients(bank);
		System.out.println("\n");
		bank = service.readBank();
		System.out.println("\n");
		System.out.println("After deserialization: ");
		service.printAllClients(bank);
		System.out.format("\n");
		service.printClient(bank,"A", "A1", Gender.MALE);
		service.printClient(bank,"A", "A1", Gender.FEMALE);
		System.out.format("\n");
		service.printBalance(bank);
		System.out.format("\n");
		service.withdrawClient(bank,"A","A1", Gender.MALE, 9);
		service.printClient(bank,"A", "A1", Gender.MALE);
		service.printClient(bank, "B", "B1", Gender.FEMALE);
		service.withdrawClient(bank,"C", "C1", Gender.MALE, 12);
		service.printClient(bank,"C", "C1", Gender.MALE);
		service.withdrawClient(bank,"C", "C1", Gender.MALE, 110);
		service.printClient(bank,"C", "C1", Gender.MALE);
		service.depositeClient(bank,"C", "C1", Gender.MALE, 200);
		service.printClient(bank,"C", "C1", Gender.MALE);
		service.withdrawClient(bank, "C", "C1", Gender.MALE, 500);
		service.printClient(bank, "C", "C1", Gender.MALE);
		service.withdrawClient(bank, "B", "B1", Gender.MALE, 500);
	}
}
